#include <iostream>
#include<algorithm>
#include <cmath>
#include<vector>
#include<cstring>
using namespace std;
int n;
vector<int> dp;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	dp.resize(10,0);
	cin >> n;
	int ans=0;
	for(int i=0;i<n;i++)
	{
	    string s;
	    cin >> s;
	    int tail=s.back()-'0';
	    int head=s[0]-'0';
	    dp[tail]=max(dp[tail],dp[head]+1);
	    ans=max(ans,dp[tail]);
	}
	cout << n - ans;
	return 0;
}